import 'package:flutter/material.dart';
import 'package:fl_chart/fl_chart.dart'; // 图表库，记得在 pubspec.yaml 添加依赖

class DashboardPage extends StatelessWidget {
  const DashboardPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("📊 仪表盘"),
        backgroundColor: Colors.blueAccent,
      ),
      body: SingleChildScrollView(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // 第一行：几个统计卡片
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                _buildStatCard("用户数", "1200", Icons.people, Colors.blue),
                _buildStatCard("订单数", "356", Icons.shopping_cart, Colors.green),
                _buildStatCard("营收", "￥98,000", Icons.attach_money, Colors.orange),
              ],
            ),
            const SizedBox(height: 20),

            // 第二行：图表
            const Text(
              "📈 销售趋势",
              style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
            ),
            const SizedBox(height: 200, child: SalesChart()),

            const SizedBox(height: 20),

            // 第三行：最近活动
            const Text(
              "📝 最近活动",
              style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
            ),
            const SizedBox(height: 10),
            _buildActivityItem("用户 A 下单", "5 分钟前"),
            _buildActivityItem("用户 B 注册", "20 分钟前"),
            _buildActivityItem("用户 C 退款", "1 小时前"),
          ],
        ),
      ),
    );
  }

  // 统计卡片
  Widget _buildStatCard(String title, String value, IconData icon, Color color) {
    return Card(
      elevation: 4,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
      child: Container(
        width: 110,
        padding: const EdgeInsets.all(12),
        child: Column(
          children: [
            Icon(icon, size: 40, color: color),
            const SizedBox(height: 8),
            Text(value, style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
            Text(title, style: const TextStyle(color: Colors.grey)),
          ],
        ),
      ),
    );
  }

  // 最近活动条目
  Widget _buildActivityItem(String content, String time) {
    return ListTile(
      leading: const Icon(Icons.event_note, color: Colors.blueAccent),
      title: Text(content),
      subtitle: Text(time),
    );
  }
}

// 销售趋势图表（柱状图示例）
class SalesChart extends StatelessWidget {
  const SalesChart({super.key});

  @override
  Widget build(BuildContext context) {
    return BarChart(
      BarChartData(
        alignment: BarChartAlignment.spaceAround,
        titlesData: FlTitlesData(
          bottomTitles: AxisTitles(
            sideTitles: SideTitles(
              showTitles: true,
              getTitlesWidget: (double value, TitleMeta meta) {
                const days = ["一", "二", "三", "四", "五", "六", "日"];
                return Text(days[value.toInt()]);
              },
            ),
          ),
          leftTitles: const AxisTitles(
            sideTitles: SideTitles(showTitles: true),
          ),
        ),
        barGroups: List.generate(7, (index) {
          return BarChartGroupData(
            x: index,
            barRods: [
              BarChartRodData(toY: (index + 1) * 10.0, color: Colors.blueAccent),
            ],
          );
        }),
      ),
    );
  }
}
